package com.ruoyi.hr.service.impl.salaryquery;


import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.mapper.ProcedureMapper;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.salaryquery.MonthSalaryHistoryMapper;
import com.ruoyi.hr.service.salaryquery.MonthSalaryHistoryService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ExcelUtils;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class MonthSalaryHistoryServiceImpl implements MonthSalaryHistoryService {

    @Autowired
    MonthSalaryHistoryMapper monthSalaryHistoryMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    ProcedureMapper procedureMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> getPayGroup(String urid) {
        return monthSalaryHistoryMapper.getPayGroup(urid);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<String> findPro(String jsid) {
        return monthSalaryHistoryMapper.findPro(jsid);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> historyList(Map map) {
        SysUser user = ShiroUtils.getSysUser();
        Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
        map.put("pro", monthSalaryHistoryMapper.findPro(map.get("jsid").toString()));
        return monthSalaryHistoryMapper.historyList(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findData(Map map) {
        SysUser user = ShiroUtils.getSysUser();
        Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
        map.put("pro", monthSalaryHistoryMapper.findPro(map.get("jsid").toString()));
        return monthSalaryHistoryMapper.findData(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public AjaxResult exportData(String jsid) {
        Map map = new HashMap();
        map.put("jsid", jsid);
        List<Map<String, Object>> listMap = this.findData(map);

        for (Map<String, Object> map1 : listMap) {
            //keySet获取map集合key的集合  然后在遍历key即可
            for (String key : map1.keySet()) {
                if (map1.get(key) == null) {
                    map1.put(key, "");
                }
            }
        }


        try {
            HSSFWorkbook wkb = new HSSFWorkbook();
            List<String> ProList = monthSalaryHistoryMapper.findPro(map.get("jsid").toString());


            String sheetname = "月薪资历史导出";
            //设置Sheet名称
            HSSFSheet sheet = wkb.createSheet(sheetname);
            //新增一行(用于列名)
            HSSFRow row1 = sheet.createRow(0);
            //设置固定列名
            row1.createCell(0).setCellValue("peid");
            row1.createCell(1).setCellValue("工号");
            row1.createCell(2).setCellValue("姓名");
            row1.createCell(3).setCellValue("公司");
            row1.createCell(4).setCellValue("部门");
            row1.createCell(5).setCellValue("岗位");
            row1.createCell(6).setCellValue("入职日期");
            row1.createCell(7).setCellValue("离职日期");
            row1.createCell(8).setCellValue("薪资城市");

            for (int i = 0; i < ProList.size(); i++) {
                sheet.setColumnWidth(i, 16 * 256);
                row1.createCell(i + 9).setCellValue(ProList.get(i));
            }
            //将数据放到Excel中
            HSSFRow row;
            for (int i = 1; i <= listMap.size(); i++) {
                //循环写入行
                row = sheet.createRow(i);
                row.createCell(0).setCellValue(listMap.get(i - 1).get("peid").toString());
                row.createCell(1).setCellValue(listMap.get(i - 1).get("badge").toString());
                row.createCell(2).setCellValue(listMap.get(i - 1).get("name").toString());
                row.createCell(3).setCellValue(listMap.get(i - 1).get("compName").toString());
                row.createCell(4).setCellValue(listMap.get(i - 1).get("depName").toString());
                row.createCell(5).setCellValue(listMap.get(i - 1).get("jobName").toString());
                row.createCell(6).setCellValue(listMap.get(i - 1).get("EmpStartDate").toString());
                row.createCell(7).setCellValue(listMap.get(i - 1).get("EmpEndDate").toString());
                row.createCell(8).setCellValue(listMap.get(i - 1).get("xzcs").toString());
                for (int j = 0; j < ProList.size(); j++) {
                    row.createCell(j + 9).setCellValue(listMap.get(i - 1).get(ProList.get(j)).toString());
                }
            }
            String filePath = Global.getDownloadPath() + sheetname + ".xls";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            wkb.write(fileOut);
            fileOut.close();
            return AjaxResult.success(0);
        } catch (Exception e) {
            return AjaxResult.error("失败");
        }
    }


    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public AjaxResult importData(MultipartFile file, String jsid) {
        //获取Excel数据  0节点为所有项目名 为String数组
        List<String[]> excelData = ExcelUtils.getExcelData(file);

        //把项目和他对应的id绑定在一个map中
        Map<String, String> proIdMap = new HashMap<String, String>();
        String proId;
        for (int i = 9; i < excelData.get(0).length; i++) {
            //根据项目和jsid取出项目id
            proId = monthSalaryHistoryMapper.getProId(excelData.get(0)[i], jsid);
            proIdMap.put(excelData.get(0)[i], proId);
        }

        //对数据循环update
        String peid;
        String id;
        String xValue;

        //先全部删除
        monthSalaryHistoryMapper.deleteData(jsid);

        for (int i = 1; i < excelData.size(); i++) {
            for (int j = 9; j < excelData.get(i).length; j++) {
                //取peid
                peid = excelData.get(i)[0];
                //取项目id
                id = proIdMap.get(excelData.get(0)[j]);
                //数值
                xValue = excelData.get(i)[j];

                monthSalaryHistoryMapper.insertDate(jsid, peid, id, xValue);
            }
        }

        return AjaxResult.success(0);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult synchronization(String jsid) {
//叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String msg;//异常信息
            sql = "exec cSP_xztb " + jsid + "," + maps.get("id").toString();
            procedureMapper.executeProcedure(sql);

        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }
}
